﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using EntitySpaces.Interfaces;
using yncTech.BusinessObjects;

public partial class Admin_FinLend_FinLend004 : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtStartDate.Value = DateTime.Now.AddMonths(-1).Date.ToString(ConstantValue.DateFormat);
            txtEndate.Value = DateTime.Now.ToString(ConstantValue.DateFormat);
            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
            if (Session["drpLeader"] != null)
            {
                drpLeader.SelectedValue = Session["drpLeader"].ToString();
            }
            if (Session["drpLoanee"] != null)
            {
                drpLoanee.SelectedValue = Session["drpLoanee"].ToString();
            }
            if (Session["drpLendType"] != null)
            {
                drpLendType.SelectedValue = Session["drpLendType"].ToString();
            }
        }

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
        Session["drpLeader"] = drpLeader.SelectedValue;
        Session["drpLoanee"] = drpLoanee.SelectedValue;
        Session["drpLendType"] = drpLendType.SelectedValue;
    }


    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        FinLendQuery q1 = new FinLendQuery("q1");
        TSCodeTableQuery q2 = new TSCodeTableQuery("q2");
        TSCodeTableQuery q3 = new TSCodeTableQuery("q3");
        BFThirdUnitQuery q4 = new BFThirdUnitQuery("q4");

        ArrayList args = new ArrayList();
        args.Add(q4.ManageUserId == LoginUserInfo.CurrentUser.UserID);
        args.Add(q1.LendStatus == "LendStatus004");

        if (!string.IsNullOrEmpty(txtStartDate.Value))
        {
            args.Add(q1.CreatedDttm >= txtStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtEndate.Value))
        {
            args.Add(q1.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        }
        if (!string.IsNullOrEmpty(drpLeader.SelectedValue))
        {
            args.Add(q1.Leader == drpLeader.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpLoanee.SelectedValue))
        {
            args.Add(q1.Loanee == drpLoanee.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpLendType.SelectedValue))
        {
            args.Add(q1.LendType == drpLendType.SelectedValue);
        }

        q1.Select(q1, q2.Value1.As("LendTypeName"), q3.Value1.As("LendStatusName")).Where(args.ToArray());
        q1.InnerJoin(q2).On(q1.LendType == q2.Code);
        q1.InnerJoin(q3).On(q1.LendStatus == q3.Code);
        q1.InnerJoin(q4).On(q1.ThirdUnitID == q4.ThirdUnitID);
        q1.OrderBy(q1.CreatedDttm.Descending);

        pageGrid.DataSource = q1.LoadDataTable();
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("sd");
        Session.Remove("ed");
        Session.Remove("drpLeader");
        Session.Remove("drpLoanee");
        Session.Remove("drpLendType");

    }

}